import { Request, Response } from 'express';
import { contentDisplayService } from './service';
import { ContentCategory, ContentType } from '../../entities/ContentDisplay';

/**
 * 获取可用的风景信息列表 (APP接口)
 */
export const getActiveSceneryList = async (req: Request, res: Response) => {
  try {
    const limit = Number(req.query.limit) || 100;
    const type = req.query.type !== undefined ? Number(req.query.type) : undefined;
    
    // 构建查询参数
    const queryParams: any = {
      category: ContentCategory.SCENERY,
      isActive: true
    };
    
    // 如果指定了类型，添加类型过滤
    if (type !== undefined) {
      // 验证类型是否有效
      if (!Object.values(ContentType).includes(type)) {
        return res.json({
          success: false,
          message: '无效的内容类型'
        });
      }
      queryParams.type = type;
    }
    
    // 获取内容列表
    const { contents } = await contentDisplayService.getContentList({
      ...queryParams,
      page: 1,
      pageSize: limit
    });
    
    // 简化返回数据结构，只包含前端需要的字段
    const simplifiedContents = contents.map(content => ({
      id: content.id,
      title: content.title,
      type: content.type,
      imageUrl: content.imageUrl,
      videoUrl: content.videoUrl,
      description: content.description,
      sortOrder: content.sortOrder
    }));
    
    res.json({
      success: true,
      data: simplifiedContents
    });
  } catch (error: any) {
    res.json({
      success: false,
      message: error.message || '获取风景信息失败'
    });
  }
};



/**
 * 获取可用的活动信息列表 (APP接口)
 */
export const getActiveActivityList = async (req: Request, res: Response) => {
  try {
    const limit = Number(req.query.limit) || 100;
    const type = req.query.type !== undefined ? Number(req.query.type) : undefined;
    
    // 构建查询参数
    const queryParams: any = {
      category: ContentCategory.ACTIVITY,
      isActive: true
    };
    
    // 如果指定了类型，添加类型过滤
    if (type !== undefined) {
      // 验证类型是否有效
      if (!Object.values(ContentType).includes(type)) {
        return res.json({
          success: false,
          message: '无效的内容类型'
        });
      }
      queryParams.type = type;
    }
    
    // 获取内容列表
    const { contents } = await contentDisplayService.getContentList({
      ...queryParams,
      page: 1,
      pageSize: limit
    });
    
    // 简化返回数据结构，只包含前端需要的字段
    const simplifiedContents = contents.map(content => ({
      id: content.id,
      title: content.title,
      type: content.type,
      imageUrl: content.imageUrl,
      videoUrl: content.videoUrl,
      description: content.description,
      sortOrder: content.sortOrder
    }));
    
    res.json({
      success: true,
      data: simplifiedContents
    });
  } catch (error: any) {
    res.json({
      success: false,
      message: error.message || '获取活动信息失败'
    });
  }
};